# vim:ts=4:sw=4:noet:
""" Platform specific tweaks """

import os
import sys
from c.base.version import InfoStrings

class SPlatform:
	def __init__(self):
		self.basePath = None
		self.configPath = None
		self.simPath = None
		if sys.platform == "win32":
			self.simExe = "cvm16.exe"
		else:
			self.simExe = "cvm16"

	def getSimCmd(self):
		if not self.simPath:
			options = [self.getBasePath()]
			if sys.platform == "linux2":
				options.append("../vm")
			elif sys.platform == "win32":
				options.append("c:/dev/foxtalk/vm")
			elif sys.platform == "darwin":
				options.append("../vm")
			for opt in options:
				op = os.path.join(opt, self.simExe)
				if os.path.exists(op):
					self.simPath = opt 
					break
			if not self.simPath:
				raise Exception("CVM16 not found")
		return os.path.join(self.simPath, self.simExe)

	def __getConfigPath(self):
		if sys.platform == "win32":
			home = os.getenv("USERPROFILE")
			return os.path.join(home,"Application Data",InfoStrings["shortname"])
		elif sys.platform == "linux2":
			home = os.getenv("HOME")
			return os.path.join(home,".%s" % InfoStrings["shortname"].lower())
		elif sys.platform == "darwin":
			home = os.getenv("HOME")
			return os.path.join(home,"Library","Preferences",InfoStrings["shortname"])

	def getConfigPath(self):
		""" The config path is the location of config files
				windows: env(USERPROFILE)\Application Data\<APP>
				mac: ~/Library/Preferences/<APP>
				linux: ~/.<APP> """
		if not self.configPath:
			self.configPath = self.__getConfigPath()
			if not os.path.exists(self.configPath):
				os.mkdir(self.configPath)
		return self.configPath

	def getConfigFilePath(self, fn):
		return os.path.join(self.getConfigPath(), fn)

	def getDefaultDir(self):
		if sys.platform == "win32":
			home = os.getenv("USERPROFILE")
			return os.path.join(home,"My Documents")
		else:
			return os.getenv("HOME")

	def __getBasePath(self):
		if sys.platform == "win32":
			# cwd works well enough on windows
			return os.getcwd()
		else:
			# this should work well enough on linux
			# but need to check mac app bundle
			path = [os.getcwd()]
			path.extend(os.getenv("PATH").split(":"))
			for p in path:
				fullpath = os.path.join(p, "cog.py")
				if os.path.exists(fullpath):
					return p
			# fallthrough to cwd
			return os.getcwd()

	def getDataPath(self):
		""" The data path is the location of data files
				windows: data dir next to <APP>.exe (c:\program files\<APP>\data)
				mac: inside <APP>.app (/Applications/<APP>.app/Resources/data)
				linux: <APP> /share/<APP> (/usr/local/share/<APP>) """
		#if sys.platform == "darwin":
		#	return self.getBasePath()
		#else:
		path = os.path.join(self.getBasePath(), "data")
		if os.path.exists(path):
			return path
		else:
			return self.getBasePath()

	def getDataFilePath(self, fn):
		return os.path.join(self.getDataPath(), fn)

	def getBasePath(self):
		""" The base path is the same directory as the application is in
				windows: same as <APP>.exe (c:\program files\<APP>)
				mac: same as <APP>.app (/Applications)
				linux: same as <APP> (/usr/local/bin) """
		if not self.basePath:
			self.basePath = self.__getBasePath()
		return self.basePath

Platform = SPlatform()
